package com.mao.action.init;

import java.util.List;

import org.apache.log4j.Logger;

import com.mao.action.BaseAction;
import com.mao.model.organization.OrganizationClass;
import com.mao.model.product.Product;
import com.mao.model.product.ProductClass;
import com.mao.model.warehouse.WareHouseClass;
import com.mao.service.impl.product.ProductCondition;
import com.mao.service.organization.IOrganizationClassService;
import com.mao.service.product.IProductClassService;
import com.mao.service.product.IProductService;
import com.mao.service.productstock.IProductStockService;
import com.mao.service.warehouse.IWareHouseClassService;
import com.mao.util.ClassUtil;

@SuppressWarnings("serial")
public class InitAction extends BaseAction {

	private IProductClassService productClassService;

	private IOrganizationClassService organizationClassService;

	private IWareHouseClassService wareHouseClassService;

	private IProductService productService;

	private IProductStockService productStockService;

	private String result;

	Logger log = Logger.getLogger(InitAction.class);

	// 初始化ProductClass的TreePath和Level
	public String initProClassTreePath() {
		// 初始化treePath的list
		List<ProductClass> tplist = productClassService.getAllProductClass();
		List<String> treePathList = null;
		ProductClass parent = null;
		String treePath = null;
		for (ProductClass pc : tplist) {
			if (pc.getTreePath() == null) {
				log.info(pc.getName() + "开始初始化TreePath");
				pc.setTreePath(ClassUtil.getTreePath(pc));
				productClassService.updateProductClass(pc);
				log.info(pc.getName() + "Treepath更新成功");
				pc.setLevel(ClassUtil.getLevel(pc.getTreePath()));
				productClassService.updateProductClass(pc);
				log.info(pc.getName() + "Level更新成功");
			}
		}
		result = "ProductClass的TreePath和Level初始化成功!";
		return SUCCESS;
	}

	// 初始化OrgClass的TreePath和Level
	public String initOrgClassTreePath() {
		// 初始化treePath的list
		List<OrganizationClass> tplist = organizationClassService
				.getAllOrganizationClass();
		List<String> treePathList = null;
		OrganizationClass parent = null;
		String treePath = null;
		for (OrganizationClass oc : tplist) {
			if (oc.getTreePath() == null) {
				log.info(oc.getName() + "开始初始化TreePath");
				oc.setTreePath(ClassUtil.getTreePath(oc));
				organizationClassService.updateOrganizationClass(oc);
				log.info(oc.getName() + "Treepath更新成功");
				oc.setLevel(ClassUtil.getLevel(oc.getTreePath()));
				organizationClassService.updateOrganizationClass(oc);
				log.info(oc.getName() + "Level更新成功");
			}
		}
		result = "OrganizationClass的TreePath和Level初始化成功!";
		return SUCCESS;
	}

	// 初始化WareHouseClass的TreePath和Level
	public String initWareHouseClassTreePath() {
		// 初始化treePath的list
		List<WareHouseClass> tplist = wareHouseClassService
				.getAllWareHouseClass();
		List<String> treePathList = null;
		WareHouseClass parent = null;
		String treePath = null;
		for (WareHouseClass wc : tplist) {
			if (wc.getTreePath() == null) {
				log.info(wc.getName() + "开始初始化TreePath");
				wc.setTreePath(ClassUtil.getTreePath(wc));
				wareHouseClassService.updateWareHouseClass(wc);
				log.info(wc.getName() + "Treepath更新成功");
				wc.setLevel(ClassUtil.getLevel(wc.getTreePath()));
				wareHouseClassService.updateWareHouseClass(wc);
				log.info(wc.getName() + "Level更新成功");
			}
		}
		result = "WareHouseClass的TreePath和Level初始化成功!";
		return SUCCESS;
	}


	public IProductClassService getProductClassService() {
		return productClassService;
	}

	public void setProductClassService(IProductClassService productClassService) {
		this.productClassService = productClassService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public IOrganizationClassService getOrganizationClassService() {
		return organizationClassService;
	}

	public void setOrganizationClassService(
			IOrganizationClassService organizationClassService) {
		this.organizationClassService = organizationClassService;
	}

	public IWareHouseClassService getWareHouseClassService() {
		return wareHouseClassService;
	}

	public void setWareHouseClassService(
			IWareHouseClassService wareHouseClassService) {
		this.wareHouseClassService = wareHouseClassService;
	}

	public IProductService getProductService() {
		return productService;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	public IProductStockService getProductStockService() {
		return productStockService;
	}

	public void setProductStockService(IProductStockService productStockService) {
		this.productStockService = productStockService;
	}

}
